package com.lc.type;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

//发送消息
//生产者
public class Producer {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {

         // 1、谁来发
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        // 2、发给谁
        producer.setNamesrvAddr("localhost:9876");
        producer.start();
         // 3、怎么发
         // 4、发什么

        //同步消息
//        for (int i = 0; i < 10; i++) {
//            String msg = "hello world rocketmq!"+i;
//            Message message = new Message("topic3", "tag1", msg.getBytes());
//            //同步的消息
//            SendResult send = producer.send(message);
//            // 5、发的结果是什么
//            System.out.println(send);
//        }


        //异步消息
//        for (int i = 0; i < 10; i++) {
//            String msg = "hello world rocketmq!异步发送"+i;
//            Message message = new Message("topic6", "tag1", msg.getBytes());
//            producer.send(message, new SendCallback() {
//                //发送成功的回调方法
//                @Override
//                public void onSuccess(SendResult sendResult) {
//                    System.out.println(sendResult);
//                }
//                //发送失败的回调方法
//                @Override
//                public void onException(Throwable throwable) {
//                    //业务逻辑
//                    System.out.println(throwable);
//                }
//            });
//            System.out.println("异步发送完成");
//        }


        //单向消息
//        String msg = "hello world rocketmq!单向消息";
//        Message message = new Message("topic6", "tag1", msg.getBytes());
        //producer.sendOneway(message);


        //延时消息
        String msg = "hello world rocketmq!单向消息";
        Message message = new Message("topic6", "tag1", msg.getBytes());
        //延时消息 分别设置每一条消息的发布时间
        message.setDelayTimeLevel(3);
        // 6、打扫战场  关闭连接
        //producer.shutdown();
    }
}
